package com.zhangkunhao.notes.controller;

import com.zhangkunhao.notes.bean.ButtonName;
import com.zhangkunhao.notes.entity.DO.User;
import com.zhangkunhao.notes.service.UserService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 注册
 *
 * @author 张坤浩
 */
public class RegisterListener extends JFrame implements ActionListener {
    private JFrame registerFrame;
    /**
     * 账号输入框对象
     */
    private JTextField nameText;
    /**
     * 密码输入框对象
     */
    private JTextField passwordText;
    private JPanel contentPane;
    private JFrame loginFrame;


    UserService userService = new UserService();

    List<User> users = userService.queryList(0);

    public RegisterListener() {
    }

    public RegisterListener(JFrame registerFrame, JTextField nameText, JTextField passwordText, JPanel contentPane, JFrame loginFrame) {
        this.registerFrame = registerFrame;
        this.nameText = nameText;
        this.passwordText = passwordText;
        this.contentPane = contentPane;
        this.loginFrame = loginFrame;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //将controller的参数传到Service层
        users = userService.queryList(0);
        if (e.getActionCommand() == ButtonName.SAVE) {
            String name = nameText.getText();
            String password = passwordText.getText();
            for (int i = 0; i < name.length(); i++) {
                if (name.charAt(i) == ' ') {
                    JOptionPane.showMessageDialog(contentPane, "用户名不能出现空格", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
            for (int i = 0; i < password.length(); i++) {
                if (password.charAt(i) == ' ') {
                    JOptionPane.showMessageDialog(contentPane, "密码不能出现空格", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
            if (name == null || "".equals(name)) {
                JOptionPane.showMessageDialog(contentPane, "请输入姓名", "系统提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
            if (password == null || "".equals(password)) {
                JOptionPane.showMessageDialog(contentPane, "请输入密码", "系统提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
            for (User value : users) {
                if (name.equals(value.getUserName())) {
                    JOptionPane.showMessageDialog(contentPane, "用户名已存在", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }

            //将controller的得到的参数传到Service层
            int registerRow = userService.updateByUserName(name, password);

            if (registerRow > 0) {
                //需要继承JFrame
                dispose();
                JOptionPane.showMessageDialog(contentPane, "添加成功，刷新可查看!");
                loginFrame.setVisible(true);
                registerFrame.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(contentPane, "操作失败", "系统提示", JOptionPane.WARNING_MESSAGE);
            }
        } else if (ButtonName.CANCEL.equals(e.getActionCommand())) {
            loginFrame.setVisible(true);
            registerFrame.setVisible(false);
            dispose();
        }
    }
}
